package com.yitiao.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yitiao.entity.TransMessage;
import com.yitiao.enums.TransMessageType;
import com.yitiao.mapper.TransMessageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class TransMessageServiceImpl implements TransMessageService {

    @Autowired
    TransMessageMapper messageMapper;

    @Value("${spring.application.name}")
    String serviceName;

    @Override
    public TransMessage messageSendReady(String exchange, String routingKey, String body) {
        final String messageId = UUID.randomUUID().toString();
        TransMessage transMessagePO = new TransMessage();
        transMessagePO.setId(messageId);
        transMessagePO.setService(serviceName);
        transMessagePO.setExchange(exchange);
        transMessagePO.setRoutingkey(routingKey);
        transMessagePO.setPayload(body);
        transMessagePO.setDate(new Date());
        transMessagePO.setSequence(0);
        transMessagePO.setType(TransMessageType.SEND);
        messageMapper.insert(transMessagePO);
        return transMessagePO;
    }

    @Override
    public void messageSendSuccess(String id) {
        messageMapper.delete(Wrappers.lambdaQuery(TransMessage.class)
                .eq(TransMessage::getId,id)
                .eq(TransMessage::getService,serviceName));

    }

    @Override
    public TransMessage messageSendReturn(String id, String exchange, String routingKey, String body) {
        return this.messageSendReady(exchange,routingKey,body);
    }

    @Override
    public void messageResend(String id) {
        TransMessage transMessagePO = messageMapper.selectOne(Wrappers.lambdaQuery(TransMessage.class)
                .eq(TransMessage::getId,id)
                .eq(TransMessage::getService,serviceName));
        transMessagePO.setSequence(transMessagePO.getSequence() + 1);
        messageMapper.updateById(transMessagePO);
    }

    @Override
    public List<TransMessage> listReadyMessages() {
        return messageMapper.selectList(Wrappers.lambdaQuery(TransMessage.class)
                .eq(TransMessage::getType,TransMessageType.SEND.name())
                .eq(TransMessage::getService,serviceName));
    }

    @Override
    public void messageDead(String id, String exchange, String routingKey, String queue, String body) {
        TransMessage transMessagePO = new TransMessage();
        transMessagePO.setId(id);
        transMessagePO.setService(serviceName);
        transMessagePO.setExchange(exchange);
        transMessagePO.setRoutingkey(routingKey);
        transMessagePO.setPayload(body);
        transMessagePO.setDate(new Date());
        transMessagePO.setSequence(0);
        transMessagePO.setType(TransMessageType.DEAD);
        messageMapper.insert(transMessagePO);
    }

    @Override
    public void messageDead(String id) {
        TransMessage transMessagePO = messageMapper.selectOne(Wrappers.lambdaQuery(TransMessage.class)
                .eq(TransMessage::getId,id)
                .eq(TransMessage::getService,serviceName));
        transMessagePO.setType(TransMessageType.DEAD);
        messageMapper.updateById(transMessagePO);
    }

    @Override
    public TransMessage messageReceiveReady(String id, String exchange, String routingKey, String queue,
                                            String body) {
        TransMessage transMessagePO = messageMapper.selectOne(Wrappers.lambdaQuery(TransMessage.class)
                .eq(TransMessage::getId,id)
                .eq(TransMessage::getService,serviceName));
        if (null == transMessagePO) {
            transMessagePO = new TransMessage();
            transMessagePO.setId(id);
            transMessagePO.setService(serviceName);
            transMessagePO.setExchange(exchange);
            transMessagePO.setRoutingkey(routingKey);
            transMessagePO.setQueue(queue);
            transMessagePO.setPayload(body);
            transMessagePO.setDate(new Date());
            transMessagePO.setSequence(0);
            transMessagePO.setType(TransMessageType.RECEIVE);
            messageMapper.insert(transMessagePO);
        } else {
            transMessagePO.setSequence(transMessagePO.getSequence() + 1);
            messageMapper.updateById(transMessagePO);
        }
        return transMessagePO;
    }

    @Override
    public void messageReceiveSuccess(String id)
    {
        messageMapper.delete(Wrappers.lambdaQuery(TransMessage.class)
                .eq(TransMessage::getId,id)
                .eq(TransMessage::getService,serviceName));
    }
}
